<?php

if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$mpurl = 'admin.php?ac=manage_group';
$uid = $_SESSION['user']['adminUid'];
$gid = '';

if ($_GET['op'] == 'setChatGroupAttribute'){
    $gid     = intval($_GET['gid']);
    $status  = intval($_GET['status']);
    if ($gid < 1){
        return FALSE;
    }
    
    $result = setApplyGroupAttribute($_SC ['apiurl'], $gid, $status);
    $msg = "操作失败!";
    if ($result == 1) {
        $msg = "操作成功!";
    }
    cpmessage($msg);
}else{
    $page   = 1;
    $status = intval($_GET['status']);
    $label  = intval($_GET['label']);
    $gid    = intval($_GET ['gid']) > 1 ? intval($_GET ['gid']) : '';
    $start  = intval($_GET ['start']) > 1 ? intval($_GET ['start']) : 0;
    $length = intval($_GET ['length']) > 1 ? intval($_GET ['length']) : 20;
    $attribute = 0;
    if ($status == 1 && $label == 1) $attribute = 101;
    if ($status == 1 && $label == 2) $attribute = 201;
    if ($status == 2 && $label == 1) $attribute = 1;
    if ($status == 2 && $label == 2) $attribute = 2;
    // 传参处理
    if (isset($_GET['page'])){
        $page  	=  intval($_GET['page']);
        $start 	=  ($page-1) * $length;
    }
    
    $groups = getApplyUpgradeGroups($_SC ['apiurl'], $uid, $gid, $attribute, $start, $length);
    $result = $groups['groups'];
    $start += $length;

    // 记录总数
    $total   = 500;
    $mpurl  .= '&page='.$page;
    $totalPages = ceil($total / $length);
    $pageurl = multitotal($page, $totalPages, $mpurl);
    $nextpurl  = $pageurl ['next'];
    $prevpurl  = $pageurl ['prev'];
    $mpurl     = $pageurl ['urls'];
    $firstpurl = $pageurl ['first'];
    $lastpurl  = $pageurl ['last'];
    
    $lengthArr = array(
            $length     => ' selected'
    );
    $labelArr = array(
            $label     => ' selected'
    );
    $statusArr = array(
            $status     => ' checked'
    );
}

function getApplyUpgradeGroups($url, $uid, $gid, $attribute, $start, $length){
    $param = array(
            'do'          => 'getApplyUpgradeGroups',
            'format'      => 'json',
            'start'       => $start,
            'length'      => $length,
            'uid'         => $uid,
            'gid'         => $gid,
            'attribute'   => $attribute,
    );

    // 请求服务器接口
    $content = get_api_content($url, $param);
    $content = json_decode($content, TRUE);
    $result = $content['resultData'];
    return $result;
}

function setApplyGroupAttribute($url, $gid, $status){
    $param = array(
            'do'           => 'setApplyGroupAttribute',
            'format'       => 'json',
            'gid'          => intval($gid),
            'attribute'    => intval($status),
    );

    // 请求服务器接口
    $content = get_api_content($url, $param);
    $content = json_decode($content, TRUE);
    $result = $content['resultData'];
    return $result;
}
